import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  categoryListItem,
  CategoryInfoItem,
  CategorySubInfoItem
} from '@/types/data'
const initCategory = [
  '居家',
  '美食',
  '服饰',
  '母婴',
  '个护',
  '严选',
  '数码',
  '运动',
  '杂项'
]
const newInitCategory = initCategory.map((item, index) => {
  return {
    id: index + '',
    name: item
  }
})
export default defineStore('category', {
  state () {
    return {
      categoryList: newInitCategory as categoryListItem[], // 分类数据
      CategoryInfo: {} as CategoryInfoItem, // 顶级类名数据
      CategorySubInfo: {} as CategorySubInfoItem // 二级分类数据
    }
  },
  actions: {
    // 获取分类数据
    async setCategoryList () {
      const res = await request.request<ApiRes<categoryListItem[]>>({
        url: '/home/category/head'
      })
      // console.log(res)
      this.categoryList = res.data.result
    },

    // 获取顶级类名数据
    async getTopCategory (id: string) {
      const res = await request.request<ApiRes<CategoryInfoItem>>({
        url: '/category',
        params: { id }
      })
      this.CategoryInfo = res.data.result
    },

    // 二级分类数据
    async categorySubFilter (id: string) {
      const res = await request.request<ApiRes<CategorySubInfoItem>>({
        url: '/category/sub/filter',
        params: { id }
      })
      // console.log(res)
      this.CategorySubInfo = res.data.result
    }
  }
})
